package main

import (
	"bufio"
	"fmt"
	"github.com/gin-gonic/gin"
	"os"
	"sync"
	"word_identify/my_error"
	"word_identify/types"
	"word_identify/word_type"
)

var wg sync.WaitGroup

func init() {
	word_type.InitKeywordMap()
	word_type.InitBoundaryMap()
	word_type.InitOperatorMap()
}
func main() {
	args := os.Args
	if len(args) < 2 {
		my_error.ARG_Error()
	}
	// 获取文件路径
	filePath := args[1]
	// 打开文件
	file, err := os.Open(filePath)
	if err != nil {
		my_error.File_Error(filePath)
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {
			fmt.Println("close file error")
		}
	}(file)
	// 读取文件，并输出文件内容
	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	words := make([][]types.Word, len(lines))
	for i := range lines {
		wg.Add(1)
		go word_type.WordCategory(i, lines[i], words, &wg)
	}
	wg.Wait()
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, words)
	})
	r.Run("0.0.0.0:8080")
}
